<?php
    // 引入配置文件（常量）
  include '../config.php';

  session_start();

   function login(){
    // 登录校验
    if(empty($_POST['email'])){
      $GLOBALS['message'] = '请输入邮箱';
      return;
    }

    if(empty($_POST['password'])){
      $GLOBALS['message'] = '请输入邮箱';
      return;
    }
    $email=$_POST['email'];
    $pwd=$_POST['password'];
    // 建立与数据库的链接，将页面的邮件和数据库的数据作对比
    $mysql=mysqli_connect(XIU_DB_HOST,XIU_DB_USER,XIU_DB_PWD,XIU_DB_NAME);
    if(!$mysql){
      exit('数据库连接失败');
    }

    $query=mysqli_query($mysql,"select * from users where email = '{$email}';");
    if(!$query){
      exit('数据库查询失败');
    }

    $res=mysqli_fetch_assoc($query);
    if(!$res){
      $GLOBALS['message'] = '用户不存在';
      return;
    }
    // 用户名校验
    if($email !== $res['email']){
      $GLOBALS['message'] = '用户名或密码错误';
      return;
    }

    if(md5($pwd) !== $res['password']){
      $GLOBALS['message'] = '用户名或密码错误';
      return;
    }
    $GLOBALS['message'] = '登陆成功';

    $_SESSION['current_login_user']=$res;
    header('location: /admin/index.php');

  }

  if($_SERVER['REQUEST_METHOD']==='POST'){
    login();
    // echo "string";
  }
 
  // 退出登录功能

  // if($_SERVER['REQUEST_METHOD'] === 'GET'  && isset($_GET['action']) && $_GET['action'] ==='logout' ){
  //   session_start();
  //   unset($_SESSION['current_login_user']);
  // }

  if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['action']) && $_GET['action'] === 'logout') {
  // 删除了登录标识
  unset($_SESSION['current_login_user']);
  
}
?>
 <!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>Sign in &laquo; Admin</title>
  <link rel="stylesheet" href="/static/assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="/static/assets/css/admin.css">
  <link rel="stylesheet" href="/static/assets/css/animate.css">
</head>
<body>
  <div class="login">
    <form class="login-wrap<?php echo isset($message) ? ' shake animated' : '';?>" action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post' autocomplete='off' novalidate>
      <img class="avatar" src="/static/assets/img/default.png">
      <!-- 有错误信息时展示 -->
      <?php if (isset($message)): ?>
      <div class="alert alert-danger">
        <?php echo $message; ?>
      </div>
      <?php endif ?>
      <div class="form-group">
        <label for="email" class="sr-only">邮箱</label>
        <input id="email" name="email" value='<?php echo isset($_POST['email']) ? $_POST['email'] : '' ;?>' type="email" class="form-control" placeholder="邮箱" autofocus>
      </div>
      <div class="form-group">
        <label for="password" class="sr-only">密码</label>
        <input id="password" name="password" type="password" class="form-control" placeholder="密码">
      </div>
      <button class="btn btn-primary btn-block" type="submit">登 录</button>
    </form>
  </div>
  <script src="/static/assets/vendors/jquery/jquery.js"></script>
  <script>
  $(function($){
    var reg =/^[0-9a-zA-Z_.-]+[@][0-9a-zA-Z_.-]+([.][a-zA-Z]+){1,2}$/;
    $('#email').on('blur',function(){
      var $value = $(this).val();
      // console.log($value);
      if(!$value || !reg.test($value)){
        return;
      }

      $.get('/admin/api/avatar.php' , {email : $value},function(res){
        if(!res){
          return;
        }
        $('.avatar').fadeOut(function() {
          $(this).on('load',function(){
             $(this).fadeIn();
          });
          $(this).attr('src',res);
        }); 
      });
    });//end on 
  });//end function
  </script>
</body>
</html>
